import datetime
import os
from loguru import logger
import pytest
from const import PATH, CASE_DIR


def get_report_path():
    report_path = PATH / "reports"  # 报告路径
    report_path.mkdir(exist_ok=True)  # 创建目录

    time_smap = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")  # 时间戳

    task_dir = report_path / f'{time_smap}'  # 任务目录
    task_dir.mkdir()  # 创建目录

    log_path = task_dir / "log"  # 日志路径
    log_path.mkdir()

    log_file = log_path / 'pytest.log'
    logger.add(log_file)  # 日志文件

    data_dir = task_dir / "data"  # 数据目录
    html_dir = task_dir / "html"  # 报告目录

    return data_dir, html_dir


def main():
    allure_dir, html_dir = get_report_path()
    # print(allure_dir, html_dir)
    pytest.main([f"{CASE_DIR}", '-sv', '--alluredir', f'{allure_dir}'])
    os.system(f"allure generate {allure_dir} -o {html_dir} --single-file")


if __name__ == '__main__':
    main()
